﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using Newtonsoft.Json;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.Department
{
    public partial class DepDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected SysDepartmentInfo _deptDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //todo 设置页面控件信息

                int uid = 0;

                Int32.TryParse(Request["uid"], out uid);

                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        _deptDb = session.Get<SysDepartmentInfo>(uid);
                    }

                    if (_deptDb != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                        //在此设置控件的初始属性
                        tb_deptCode.Text = _deptDb.DeptCode;
                        tb_deptName.Text = _deptDb.DeptName;
                        tb_content.Value = _deptDb.Memo;
                    }
                }
            }
            else
            {
                //todo 保存数据

                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                SysDepartmentInfo _deptDb = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        _deptDb = session.Get<SysDepartmentInfo>(uid);
                    }
                    if (_deptDb == null)
                    {
                        _deptDb = new SysDepartmentInfo();
                    }
                    _deptDb.DeptCode = tb_deptCode.Text;
                    _deptDb.DeptName = tb_deptName.Text;
                    _deptDb.Memo = tb_content.Value;

                    session.SaveOrUpdate(_deptDb);

                    session.Flush();
                }
                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/RightConfig/Department/DepDetail.aspx?tabid=DepDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + _deptDb.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}
